package JZ22_从上往下打印二叉树;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
    this.val = val;
 }

 }
public class Solution {
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        if (root == null){
            return list;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (queue.size() != 0){
            TreeNode temp = queue.poll();
            list.add(temp.val);

            if (temp.left != null){
                queue.add((temp.left));
            }
            if (temp.right != null){
                queue.add(temp.right);
            }
        }
        return list;
    }
}